//
//  HYAlert.m
//  YiCuiMiaoZhang
//
//  Created by BZ on 2023/10/20.
//

#import "HYAlert.h"

#define Public_isPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
// 屏幕大小
#define Public_SCREEN_BOUNDS ([UIScreen mainScreen].bounds)
// 屏幕宽度
#define Public_SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
// 屏幕高度
#define Public_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
// 状态栏高度
#define Public_STATUS_BAR_HEIGHT (Public_iPhoneX ? 44.f : 20.f)
// 导航栏高度
#define Public_NAVIGATION_BAR_HEIGHT (Public_iPhoneX ? 88.f : 64.f)
@implementation HYAlert


+ (HYAlert *)showToast:(NSString *)toastStr Dismiss:(BOOL )dismiss OffsetCenterY:(CGFloat)offsetCenterY Completion:(void (^)(void))completion
{
    HYAlert *alert = [[HYAlert alloc] init];
    HYAlertModel *alertModel = [[HYAlertModel alloc] init];
    alertModel.toast = toastStr;
    
    HYAlertView *alertView = [[HYAlertView alloc] initWithToast:alertModel OffsetCenterY:offsetCenterY];
    
    alert.alertModel = alertModel;
    alert.alertView = alertView;
    if(dismiss){
        [alertView showToastDismissCompletion:completion];
    }else{
        [alertView showToastWithoutDismiss];
    }

    return alert;
}

+ (HYAlert *)showToast:(NSString *)toastStr Completion:(void (^)(void))completion
{
    return [self showToast:toastStr Dismiss:YES OffsetCenterY:0.0 Completion:completion];
}

+ (HYAlert *)showToastWithoutDismiss:(NSString *)toastStr
{
    return [self showToast:toastStr Dismiss:NO OffsetCenterY:0.0 Completion:nil];
}

+ (HYAlert *)showToast:(NSString *)toastStr OffsetCenterY:(CGFloat)offsetCenterY
{
    return [self showToast:toastStr Dismiss:YES OffsetCenterY:offsetCenterY Completion:nil];
}

+ (HYAlert *)showToast:(NSString *)toastStr
{
    return [self showToast:toastStr Completion:nil];
}



+ (HYAlert *)showAlertMainTemplateWithAlertModel:(HYAlertModel *)alertModel mainChangeableView:(UIView *)changeableView
{
    HYAlert *alert = [[HYAlert alloc] init];
    alert.alertModel = alertModel;
    HYAlertView *alertView = [[HYAlertView alloc] initWithAlertModel:alertModel mainChangeableView:changeableView];
    alert.alertView = alertView;
    [alertView showAlert];
    return alert;
}


+ (HYAlert *)showAlertWithAlertModel:(HYAlertModel *)alertModel
{
    UIView *changeableView = [[UIView alloc] init];
    return [self showAlertMainTemplateWithAlertModel:alertModel mainChangeableView:changeableView];
    
}

+ (HYAlert *)showAlertWithTitle:(NSString *)title AttributedContent:(NSAttributedString *)attributedContent CancelEvent:(void (^)(void))cancelEvent ConfirmEvent:(void (^)(void))confirmEvent
{
    HYAlertModel *alertModel = [[HYAlertModel alloc] initWithTitle:title AttributedContent:attributedContent CancelEvent:^(NSMutableDictionary *stashValue) {
        if (cancelEvent) {
            cancelEvent();
        }
    } ConfirmAction:^(NSMutableDictionary *stashValue) {
        if (confirmEvent) {
            confirmEvent();
        }
    }];
    alertModel.contentAlignment = NSTextAlignmentCenter;
    return [self showAlertWithAlertModel:alertModel];
}

+ (HYAlert *)showRemindWithTitle:(NSString *)title AttributedContent:(NSAttributedString *)attributedContent KnowEvent:(void (^)(void))knowEvent
{
    HYAlertModel *alertModel = [[HYAlertModel alloc] initWithTitle:title AttributedContent:attributedContent KnowEvent:^(NSMutableDictionary *stashValue) {
        if (knowEvent) {
            knowEvent();
        }
    }];
    alertModel.contentAlignment = NSTextAlignmentLeft;
    return [self showAlertWithAlertModel:alertModel];
}


+ (UIViewController *)getCurrentViewController {
    UIViewController *currentViewController =
        [[[UIApplication sharedApplication] delegate] window].rootViewController;
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else if ([currentViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *navigationController =
                (UINavigationController *)currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
        } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController *tabBarController = (UITabBarController *)currentViewController;
            currentViewController                = tabBarController.selectedViewController;
        } else {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            if (childViewControllerCount > 0) {
                currentViewController = currentViewController.childViewControllers.lastObject;
                return currentViewController;
            } else {
                return currentViewController;
            }
        }
    }
    return currentViewController;
}






@end

@interface HYAlertView()<HYAlertModelDelegate>

@property (nonatomic, weak) UIView *mainAlertView;

@property (nonatomic, weak) UILabel *contentBackgroundLab;

@property (nonatomic, strong) HYAlertModel *alertModel;

@end

@implementation HYAlertView


- (instancetype)initWithToast:(HYAlertModel *)alertModel OffsetCenterY:(CGFloat)offsetCenterY
{
    self = [super init];
    if (self) {
        self.frame = [UIApplication sharedApplication].keyWindow.bounds;
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0;
        self.userInteractionEnabled = NO;
        self.alertModel = alertModel;
        
        UIView *toastGrayView = [[UIView alloc] init];
        toastGrayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        toastGrayView.layer.cornerRadius = 6.0;
        toastGrayView.userInteractionEnabled = NO;
        toastGrayView.layer.masksToBounds = YES;
        [self addSubview:toastGrayView];
        [toastGrayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).offset(offsetCenterY);
            make.width.mas_equalTo(Public_isPad ? 480: Public_SCREEN_WIDTH - 160);
        }];
        
        UILabel *toastLab = [[UILabel alloc] init];
        toastLab.textColor = [UIColor blackColor];
        toastLab.font = [UIFont systemFontOfSize:15.];
        toastLab.text = alertModel.toast;
        toastLab.userInteractionEnabled = NO;
        toastLab.textAlignment = NSTextAlignmentCenter;
        toastLab.numberOfLines = 0.;
        [toastGrayView addSubview:toastLab];
        [toastLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(toastGrayView).offset(10);
            make.right.equalTo(toastGrayView).offset(-10);
            make.top.equalTo(toastGrayView).offset(20);
            make.bottom.equalTo(toastGrayView).offset(-20);
        }];
    }
    return self;
    
}

- (instancetype)initWithAlertModel:(HYAlertModel *)alertModel mainChangeableView:(UIView *)changeableView
{
    self = [super init];
    if (self) {
        self.alertModel = alertModel;
        self.frame = [UIApplication sharedApplication].keyWindow.bounds;
        
        self.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.6];;
        self.userInteractionEnabled = YES;
        
        UIView *mainAlertView = [[UIView alloc] init];
        mainAlertView.backgroundColor = [UIColor orangeColor];
        mainAlertView.layer.cornerRadius = 5;
        mainAlertView.layer.masksToBounds = YES;
        [self addSubview:mainAlertView];
        self.mainAlertView = mainAlertView;
        [mainAlertView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).offset(-100);
            make.width.mas_equalTo(Public_isPad ? 300: Public_SCREEN_WIDTH - 100);
            make.height.mas_lessThanOrEqualTo(Public_SCREEN_HEIGHT - 100);
        }];
        
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.textColor = [UIColor blackColor];
        titleLab.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.];
        titleLab.text = alertModel.title;
        titleLab.textAlignment = NSTextAlignmentCenter;
        [mainAlertView addSubview:titleLab];
        self.titleLab = titleLab;
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(mainAlertView);
            make.top.equalTo(mainAlertView).offset(alertModel.title.length> 0? 15:0);
        }];
        
        //利用底色制造分割线
        UIView *bottomView = [[UIView alloc] init];
        bottomView.backgroundColor = [UIColor lightGrayColor];
        [mainAlertView addSubview:bottomView];
        self.bottomView = bottomView;
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(mainAlertView);
            make.height.mas_equalTo(51);
        }];
        
        UIStackView *bottomBtnView = [[UIStackView alloc] init];
        bottomBtnView.axis = UILayoutConstraintAxisHorizontal;
        bottomBtnView.alignment = UIStackViewAlignmentFill;
        bottomBtnView.distribution = UIStackViewDistributionFillEqually;
        bottomBtnView.spacing = 1;
        [bottomView addSubview:bottomBtnView];
        [bottomBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(mainAlertView);
            make.top.equalTo(bottomView).offset(1);
        }];
        
        __weak typeof(self)weakSelf = self;
        [alertModel.bottomActions enumerateObjectsUsingBlock:^(HYAlertActionModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor lightGrayColor];;
            [btn setTitleColor:obj.titleColor forState:UIControlStateNormal];
            [btn setTitle:obj.actionTitle forState:UIControlStateNormal];
            if(obj.timeCountDown){
                [btn setTitle:[NSString stringWithFormat:@"%@(%ld)", obj.countDownTitle, (long)obj.countDownNumber] forState:UIControlStateNormal];
                [btn setTitleColor:obj.countDownTitleColor forState:UIControlStateNormal];
                __weak typeof(obj)weakObj = obj;
                btn.enabled = NO;
                obj.timeCountDownEvent = ^(BOOL finish) {
                    if (finish) {
                        btn.enabled = YES;
                        [btn setTitle:weakObj.actionTitle forState:UIControlStateNormal];
                        [btn setTitleColor:weakObj.titleColor forState:UIControlStateNormal];
                    }else{
                        [btn setTitle:[NSString stringWithFormat:@"%@(%ld)", weakObj.countDownTitle, (long)weakObj.countDownNumber] forState:UIControlStateNormal];
                        [btn setTitleColor:weakObj.countDownTitleColor forState:UIControlStateNormal];
        
                    }
                };
                [obj startTimer];
            }
            
            
//            btn.click = ^{
//                if (obj.withoutDismissActionEvent) {
//                    obj.withoutDismissActionEvent(alertModel.stashValue,weakSelf);
//                    return;
//                }
//                
//                if(obj.actionEvent){//按钮本身需要处理的事件
//                    obj.actionEvent(alertModel.stashValue);
//                }
//                
//                [weakSelf dismissAlert];
//            };
            [bottomBtnView addArrangedSubview:btn];
            
        }];
        
        [mainAlertView addSubview:changeableView];
        self.changeableView = changeableView;
        [changeableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(mainAlertView).offset(10);
            make.right.equalTo(mainAlertView).offset(-10);
            make.bottom.equalTo(bottomView.mas_top).offset(-15);
        }];
        
        
        UIView *contentView = [[UIView alloc] init];
        [mainAlertView addSubview:contentView];
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(mainAlertView).offset(10);
            make.right.equalTo(mainAlertView).offset(-10);
            make.top.equalTo(titleLab.mas_bottom).offset(15);
            make.bottom.equalTo(changeableView.mas_top).offset(-5);
        }];
        
        //背后放一个文本让他撑开内容来确定scrollView的控件大小，并且约束根据外层约束限制scrollView的最大高度
        UILabel *contentBackgroundLab = [[UILabel alloc] init];
        contentBackgroundLab.numberOfLines = 0;
        contentBackgroundLab.font = [UIFont systemFontOfSize:14.];
        contentBackgroundLab.textAlignment = alertModel.contentAlignment;
        contentBackgroundLab.attributedText = alertModel.attributedContent;
        
        contentBackgroundLab.textColor = [UIColor clearColor];
        [contentView addSubview:contentBackgroundLab];
        self.contentBackgroundLab = contentBackgroundLab;
        [contentBackgroundLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(contentView);
        }];
        
        
        UIScrollView *contentScrollView = [[UIScrollView alloc] init];
        contentScrollView.showsVerticalScrollIndicator = NO;
        [contentView addSubview:contentScrollView];
        [contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(contentBackgroundLab);
        }];
        
        UILabel *contentLab = [[UILabel alloc] init];
        contentLab.textColor = [UIColor blackColor];
        contentLab.font = [UIFont systemFontOfSize:14.];
        contentLab.textAlignment = alertModel.contentAlignment;
        contentLab.attributedText = alertModel.attributedContent;
        contentLab.numberOfLines = 0;

        [contentScrollView addSubview:contentLab];
        self.contentLab = contentLab;
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(contentScrollView);
            make.width.mas_equalTo(contentView);
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDismiss:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)updateAttributedContent:(NSAttributedString *)attributedContent
{
    self.contentBackgroundLab.attributedText = attributedContent;
    self.contentLab.attributedText = attributedContent;
    self.alertModel.attributedContent = attributedContent;
    [self layoutIfNeeded];
}

- (void)keyboardWillShow:(NSNotification *)note {
    self.mainAlertView.transform = CGAffineTransformIdentity;
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration;
    CGRect keyboardF;
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardF];
    CGFloat viewY = CGRectGetMaxY(self.mainAlertView.frame);
    CGFloat keyY = CGRectGetMinY(keyboardF);
    if (viewY > keyY) {
        [UIView animateWithDuration:duration animations:^{
            self.mainAlertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, keyY-viewY);
        }];
    }
}

- (void)keyboardWillDismiss:(NSNotification *)note {
    self.mainAlertView.transform = CGAffineTransformIdentity;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

    NSLog(@"-----dealloc:HYAlertView----");
    
    
}

- (void)showToastWithoutDismiss
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.;
    }completion:^(BOOL finished) {
        
    }];
}

- (void)showToastDismissCompletion:(void (^)(void))completion
{
    CGFloat time = (0.5 + (self.alertModel.toast.length / 10) * 0.2);
    time = time > 2 ? 2 : time;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.;
    }completion:^(BOOL finished) {
        [self dismissToastAnimated:YES Completion:completion];
    }];
}

- (void)dismissToastAnimated:(BOOL)animated
{
    [self dismissToastAnimated:animated Completion:nil];
}

- (void)dismissToastAnimated:(BOOL)animated Completion:(void (^)(void))completion
{
    if(animated){
        CGFloat time = (0.5 + (self.alertModel.toast.length / 10) * 0.2);
        time = time > 2 ? 2 : time;
        [UIView animateWithDuration:0.5 animations:nil completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.5 animations:^{
                    self.alpha = 0.0;
                } completion:^(BOOL finished) {
                    [self removeFromSuperview];
                }];
            });
        }];
    }else{
        self.alpha = 0;
        [self removeFromSuperview];
        if (completion) {
            completion();
        }
    }
}



- (void)showAlert
{
    self.alertModel.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.mainAlertView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismissAlert
{
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.mainAlertView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(-200);
        }];
        [self layoutIfNeeded];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)alertDismissWithAlertModel:(HYAlertModel *)alertModel
{
    [self dismissAlert];
}






@end

@implementation HYAlertCustomChangeableView

+ (UIView *)createSingleCheckBoxChangeableViewWithAlertModel:(HYAlertModel *)alertModel Title:(NSString *)title Selected:(BOOL )selected
{
    [alertModel.stashValue setValue:@(selected) forKey:@"selected"];
    UIView *changeableView = [[UIView alloc] init];
    if(title.length > 0){
        UILabel *checkBoxLab = [[UILabel alloc] init];
        checkBoxLab.textColor = [UIColor blackColor];
        checkBoxLab.font = [UIFont systemFontOfSize:13.];
        checkBoxLab.numberOfLines = 0.;
        checkBoxLab.text = title;
        [changeableView addSubview:checkBoxLab];
        [checkBoxLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(changeableView);
            make.top.equalTo(changeableView).offset(5);
            make.bottom.equalTo(changeableView).offset(-5);
            make.left.equalTo(changeableView).offset(30);
        }];

        UIButton *checkBtn = [[UIButton alloc] init];
//        [checkBtn setImage:BZTHEME_IMAGE(skin_checkbox2_default_img) forState:UIControlStateNormal];
//        [checkBtn setImage:BZTHEME_IMAGE(skin_checkbox2_selected_img) forState:UIControlStateSelected];
//        checkBtn.selected = selected;
//        checkBtn.clickReturn = ^(UIButton *btn) {
//            btn.selected = !btn.selected;
//            [alertModel.stashValue setValue:@(btn.selected) forKey:@"selected"];
//        };
        [changeableView addSubview:checkBtn];
        [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(changeableView).offset(2);
            make.centerY.equalTo(checkBoxLab);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
    }
    return changeableView;
}

@end


@implementation HYAlertModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentAlignment = NSTextAlignmentLeft;
        self.stashValue = [NSMutableDictionary dictionary];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title AttributedContent:(NSAttributedString *)attributedContent {
    self = [self init];
    if(self){
        self.title = title;
        self.attributedContent = attributedContent;
    }
    return self;
}


- (instancetype)initWithTitle:(NSString *)title AttributedContent:(NSAttributedString *)attributedContent CancelEvent:(void (^)(NSMutableDictionary *stashValue))cancelEvent ConfirmAction:(void (^)(NSMutableDictionary *stashValue))confirmEvent
{
    self = [self initWithTitle:title AttributedContent:attributedContent];
    if (self) {
        HYAlertActionModel *cancelAction = [[HYAlertActionModel alloc] initWithActionTitle:@"取消" ActionEvent:cancelEvent];
        HYAlertActionModel *confirmAction = [[HYAlertActionModel alloc] initWithActionTitle:@"确认" ActionEvent:confirmEvent];
        self.bottomActions = @[cancelAction,confirmAction];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title AttributedContent:(NSAttributedString *)attributedContent KnowEvent:(void (^)(NSMutableDictionary *stashValue))knowEvent
{
    self = [self initWithTitle:title AttributedContent:attributedContent];
    if (self) {
        HYAlertActionModel *knowAction = [[HYAlertActionModel alloc] initWithActionTitle:@"我知道了" ActionEvent:knowEvent];
        self.bottomActions = @[knowAction];
    }
    return self;
}

- (void)createBottomButtonArrayWithSingleActionTitle:(NSString *)actionTitle ActionEvent:(void (^)(NSMutableDictionary *stashValue))actionEvent
{
    HYAlertActionModel *actionModel = [[HYAlertActionModel alloc] initWithActionTitle:actionTitle ActionEvent:actionEvent];
    self.bottomActions = @[actionModel];
}

- (void)creatBottomButtonArrayWithTwoActionFirstTitle:(NSString *)firstTitle FirstEvent:(void (^)(NSMutableDictionary *stashValue))firstEvent SecondTitle:(NSString *)secondTitle SecondEvent:(void (^)(NSMutableDictionary *stashValue))secondEvent
{
    HYAlertActionModel *firstModel = [[HYAlertActionModel alloc] initWithActionTitle:firstTitle ActionEvent:firstEvent];
    HYAlertActionModel *secondModel = [[HYAlertActionModel alloc] initWithActionTitle:secondTitle ActionEvent:secondEvent];
    self.bottomActions = @[firstModel,secondModel];
}

- (void)dismissAlert
{
    if([self.delegate respondsToSelector:@selector(alertDismissWithAlertModel:)]){
        [self.delegate alertDismissWithAlertModel:self];
    }
}

- (void)dealloc
{
    NSLog(@"-----dealloc:HYAlertModel----");
}








@end


@interface HYAlertActionModel()

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation HYAlertActionModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.countDownNumber = 10;
        self.titleColor = [UIColor blackColor];
        self.countDownTitleColor = [UIColor lightTextColor];
        
    }
    return self;
}

- (instancetype)initWithActionTitle:(NSString *)actionTitle ActionEvent:(void (^)(NSMutableDictionary *stashValue))actionEvent
{
    self = [self init];
    if (self) {
        self.actionTitle = actionTitle;
        self.actionEvent = actionEvent;
    }
    return self;
}


- (instancetype)initWithActionTitle:(NSString *)actionTitle WithoutDismissActionEvent:(void (^)(NSMutableDictionary *stashValue,HYAlertView *alertView))withoutDismissActionEvent
{
    self = [self init];
    if (self) {
        self.actionTitle = actionTitle;
        self.withoutDismissActionEvent = withoutDismissActionEvent;
    }
    return self;
}

- (instancetype)initWithTimeDownActionTitle:(NSString *)actionTitle ActionEvent:(void (^)(NSMutableDictionary *stashValue))actionEvent
{
    self = [self initWithActionTitle:actionTitle ActionEvent:actionEvent];
    if(self){
        self.countDownTitle = actionTitle;
        self.titleColor = [UIColor redColor];
        self.timeCountDown = true;
    }
    return self;
}



- (void)startTimer{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMinus) userInfo:nil repeats:YES];
    
}

- (void)timeMinus
{
    self.countDownNumber --;
    self.timeCountDownEvent(self.countDownNumber <= 0);
    if (self.countDownNumber <= 0) {
        [self.timer invalidate];
    }
}



@end
